package com.idbk.chargingstation.smdal.entity;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import com.idbk.common.dal.DBSession;
import com.idbk.common.dal.DaoBase;
import com.idbk.common.dal.EOrderOptionEnum;
import com.idbk.common.dal.ESearchOptionEnum;
import com.idbk.common.dal.JDBCHelp;
import com.idbk.common.dal.JDBCHelpFactory;
import com.idbk.common.dal.OrderOption;
import com.idbk.common.dal.SearchOption;
import com.idbk.common.dal.SearchPager;
import com.idbk.common.util.DisposeUtil;

public class AlarmEventDao extends DaoBase {

    public AlarmEvent getById(Integer id) throws SQLException {
		return (AlarmEvent)super.getById(AlarmEvent.class, id);
	}

	public void save(AlarmEvent data) throws SQLException {
		if(data.getId() != null && data.getId() > 0){
			update(data);
			return;
		}
		super.saveEntity(data);
	}
	
	public void update(AlarmEvent data) throws SQLException {
		super.updateEntity(data);
	}
	
	public Collection<AlarmEvent> getUnfinished() throws SQLException {
		
		Collection<AlarmEvent> result = new LinkedList<AlarmEvent>();
		
		JDBCHelp jdbcHelp = JDBCHelpFactory.getJDBCHelp();
		String sql = jdbcHelp.getSearchOptionSql(
				AlarmEvent.class, 
				new SearchOption[]{
					new SearchOption(ESearchOptionEnum.Equals, "IsFinished", false)
				});
		PreparedStatement cmd = null;
		ResultSet rs = null;
		
		try{
			cmd = DBSession.getPreparedStatement(sql);
			jdbcHelp.setParameterForSearchOption(cmd, null);
			rs = cmd.executeQuery();
			while(rs.next()){
				AlarmEvent item = new AlarmEvent();
				jdbcHelp.readFields(rs, item);
				result.add(item);
			}
		}finally{
			DisposeUtil.safeClose(rs);
			DisposeUtil.safeClose(cmd);
		}
		
		return result;
	}
	
	
	public Collection<AlarmEvent> getAlarmEventByPager(SearchPager sp,Collection<Integer> deviceId,AlarmEvent alarm) {
		Collection<AlarmEvent> result = new ArrayList<AlarmEvent>();
		JDBCHelp jdbcHelp = JDBCHelpFactory.getJDBCHelp();
		OrderOption[] oos = new OrderOption[]{
				new OrderOption("IsFinished", EOrderOptionEnum.Asc),
				new OrderOption("AlarmBeginDateTime", EOrderOptionEnum.Desc)
		}; 
		
		SearchOption[] sos = new SearchOption[] {
				new SearchOption(ESearchOptionEnum.In,"DeviceId",deviceId),
				new SearchOption(ESearchOptionEnum.GreatThan,"CreatedDateTime",alarm.getAlarmBeginDateTime()),
				new SearchOption(ESearchOptionEnum.LessThanEquals,"CreatedDateTime",alarm.getFinishedDateTime())
		};
		
		String sql = jdbcHelp.getSearchOptionSql(AlarmEvent.class,sos, oos);
		String countSql = jdbcHelp.toGetCountSql(sql);
		String pageSql = jdbcHelp.toPageSql(sql, sp.getPageIndex(),
				sp.getPageSize());
		PreparedStatement prst = null;
		ResultSet rs = null;
		try {
			prst = DBSession.getPreparedStatement(countSql);
			jdbcHelp.setParameterForSearchOption(prst, sos);
			rs = prst.executeQuery();
			while (rs.next()) {
				sp.reset(rs.getInt(1));
			}

			prst = DBSession.getPreparedStatement(pageSql);
			jdbcHelp.setParameterForSearchOption(prst, sos);
			rs = prst.executeQuery();
			while (rs.next()) {
				AlarmEvent alarmEvent = new AlarmEvent();
				jdbcHelp.readFields(rs, alarmEvent);
				result.add(alarmEvent);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DisposeUtil.safeClose(rs);
			DisposeUtil.safeClose(prst);
		}
		return result;
	}

	
}
